/*
 * Copyright (C) 2016 The Android Open Source Project
 *  Created by  Ant  2016/8/29  16:18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ant.view.cn.view001;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.ant.view.cn.beans.ViewMasterBean;
import com.ant.view.cn.utils.AntLog;

import java.util.Timer;

/**
 * Created by baichuan on 2016/8/29.
 */
public class View001 extends View implements View.OnClickListener,Runnable{
    private ViewMasterBean bean;
    private int radus = 0;

    public View001(Context context) {
        super(context);
        initview();
    }

    public View001(Context context, AttributeSet attrs) {
        super(context, attrs);
        initview();
    }

    public View001(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initview();
    }

    public void initview() {
        bean = new ViewMasterBean();
        this.setOnClickListener(this);
        Timer timer=new Timer();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        ;
        radus = Math.min(width, height) / 2;

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
//        canvas.drawColor(Color.BLACK);
        for (int i = radus; i >=2; i--) {
            int rr = (int) (Math.random() * 255);
            int gg = (int) (Math.random() * 255);
            int bb = (int) (Math.random() * 255);
            AntLog.i("ant","rr="+rr+", gg="+gg+",  bb="+bb);
            paint.setColor(Color.rgb(rr, gg, bb));
            canvas.drawCircle(radus, radus, i, paint);
        }

    }

    @Override
    public void onClick(View view) {
        if (view == this) {
            bean.setWidth(this.getWidth());
            bean.setHeight(this.getHeight());
            bean.setX(getX());
            bean.setY(getY());
            bean.setTop(getTop());
            bean.setLeft(getLeft());
            AntLog.i("ant", bean.toString());
            this.invalidate();
        }
    }


    @Override
    public void run() {
        this.postInvalidate();
    }
}
